package models

import (
	"fmt"
	//"time"

	"github.com/jinzhu/gorm"
)

type PerformanceMs struct {
	gorm.Model
	PlanID                   string         `gorm:"column:planid"`
	InterfaceName              string         `gorm:"column:interfacename"`
	MaxUsers                  string         `gorm:"column:maxusers"`
	AvgThroughput                 string         `gorm:"column:avgthroughput"`
	PlanStatus                bool		     `gorm:"column:planstatus"`
	Errors                    string		 `gorm:"column:errors"`
	AvgResponseTime              string        `gorm:"column:avgResponsetime"`
	ResponseTime90              string         `gorm:"column:responsetime90"`
	AvgBandwidth              string         `gorm:"column:avgbandwidth"`
	AvgTransactions             string         `gorm:"column:avgtransactions"`
    
}

func (PerformanceMs) TableName() string {
	return "Performance_ms"
}

func (*PerformanceMs) Create(planid string) {
	plan := PerformanceMs{}
	plan.PlanID = planid
	plan.PlanStatus = false
	db.Create(&plan)
}


func (*PerformanceMs) Update(planid string, plan PerformanceMs) {
	//fmt.Printf("修改%v的状态", plan)
	plannew := PerformanceMs{}
	db.Model(&plannew).Where("planid=?",planid).Updates(&plan)
}


func (*PerformanceMs) UpdateStatus(planid string) {
	plan := PerformanceMs{}
	plan.PlanID = planid
	//fmt.Printf("修改%v的状态", plan)
	db.Model(&plan).Where("planid=?",planid).Update("PlanStatus", true)
}

func (*PerformanceMs) Get(planid string) PerformanceMs {
	plan := PerformanceMs{}
	//fmt.Println("开始查找:", planid)
	db.Where("planid=?",planid).First(&plan)
	//fmt.Printf("plan:%+v", plan)
	return plan

}

func (*PerformanceMs) GetList() []PerformanceMs {
	plans := []PerformanceMs{}
	db.Order("planid desc").Limit(20).Find(&plans)
	//fmt.Printf("plan:%+v", plans)
	return plans

}

func (*PerformanceMs) Delete(PlanID string) {
	plan := PerformanceMs{}
	fmt.Println("delete recode:", PlanID)
	db.Where("planid=?",PlanID).Delete(plan)	
}

func (*PerformanceMs)GetExectTime(planid string) float64 {
	plan := PerformanceMs{}
	//fmt.Println("开始查找:", planid)
	db.Where("planid=?",planid).First(&plan)
	starttime := plan.CreatedAt
	endtime := plan.UpdatedAt
	execttime := endtime.Sub(starttime).Seconds()	
	return execttime
}